package view;

import Model.Obj.User;
import Model.OrcUtils.JDBCUtils;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;

public class PowerGUI extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private DefaultTableModel dtm;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    PowerGUI frame = new PowerGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public PowerGUI() {
        setTitle("\u8D85\u7EA7\u7BA1\u7406\u5458");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 350, 302);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JButton btnNewButton = new JButton("\u5220\u9664\u7BA1\u7406\u5458");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                deleteactionPerformed(e);
            }
        });

        JButton btnNewButton_1 = new JButton("\u65B0\u5EFA\u7BA1\u7406\u5458");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createactionPerformed(e);
            }
        });

        JScrollPane scrollPane = new JScrollPane();
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                        .addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
                                                .addComponent(btnNewButton)
                                                .addGap(102)
                                                .addComponent(btnNewButton_1))
                                        .addComponent(scrollPane, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 288, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(102, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(btnNewButton_1)
                                        .addComponent(btnNewButton))
                                .addGap(52))
        );

        table = new JTable();
        String[] head = {"用户名","密码"};
        String[][] data = new String[][]{};
        dtm = new DefaultTableModel(data,head){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        String sql = "select * from USERDATA where POWER=?";
        List<User> list = JDBCUtils.JDBCselect(sql,User.class,"普通管理员");
        for (User user : list){
            Vector v = new Vector<>();
            v.add(user.getUSERNAME());
            v.add(user.getPASSWORD());
            dtm.addRow(v);
        }
        table.setModel(dtm);
        scrollPane.setViewportView(table);
        contentPane.setLayout(gl_contentPane);
    }

    private void createactionPerformed(ActionEvent e) {
        new CreateUser().setVisible(true);
    }

    private void deleteactionPerformed(ActionEvent e) {
        int row = table.getSelectedRow();
        String name = (String) table.getValueAt(row,0);
        String sql = "delete from USERDATA where USERNAME=?";
        int count = JDBCUtils.DataChange(sql,name);
        if (count >= 0 ){
            JOptionPane.showMessageDialog(null,"删除成功");
        } else {
            JOptionPane.showMessageDialog(null,"删除失败");
        }
    }
}
